package project;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;

public class EmpPanel extends JPanel implements ActionListener
{
	private JPanel bigButtonP = null, mainP = null;
	private JButton addEmpB = null, lookEmpB = null,
					spareB = null;
	private int accessLevel = 3;
	AddEmpPanel acp;
	LookEmpPanel lcp;
	Connection conn;
	DetailsPanel dp;
	AddressPanel ap1;
	
	public EmpPanel(Connection conn)
	{
		this.conn = conn;
		this.setLayout(new BorderLayout());
		spareB = new JButton();
		bigButtonP = new JPanel(new GridLayout(1,2));
		addEmpB = new JButton("Add New Employee");
		addEmpB.addActionListener(this);
		addEmpB.setFocusPainted(false);
		addEmpB.setBorderPainted(false);
		addEmpB.setBackground(new Color(215,215,215));
		lookEmpB = new JButton("Look Up Employee");
		lookEmpB.addActionListener(this);
		lookEmpB.setFocusPainted(false);
		lookEmpB.setBorderPainted(false);
		lookEmpB.setBackground(new Color(238,238,238));
		bigButtonP.add(lookEmpB);
		bigButtonP.add(addEmpB);

		bigButtonP.setPreferredSize(new Dimension(780, 30));

		
		mainP = new JPanel(new GridLayout(1,1));
		lcp = new LookEmpPanel(conn);
		mainP.add(lcp);
		dp = lcp.getDetailsPanel();
		ap1 = lcp.getAddressPanel();
		
		this.add(bigButtonP, BorderLayout.NORTH);
		this.add(mainP, BorderLayout.CENTER);
	
	}
	
	public void actionPerformed(ActionEvent e)
	{
		Object source = e.getSource();
		if(source == lookEmpB)
		{
			LookEmpPanel lcp = new LookEmpPanel(conn);
			addEmpB.setBackground(new Color(215,215,215));
			lookEmpB.setBackground(new Color(238,238,238));
			mainP.removeAll();
			mainP.add(lcp);
			this.validate();
			this.repaint();
		}
		if(source == addEmpB)
		{
			acp = new AddEmpPanel(conn);
			lookEmpB.setBackground(new Color(215,215,215));
			addEmpB.setBackground(new Color(238,238,238));
			mainP.removeAll();
			mainP.add(acp);
			this.validate();
			this.repaint();
		}
	}
	public int getAccessLevel()
	{
		return accessLevel;
	}
	public void emptyAll()
	{
		dp.empIDT.setText("");
		dp.surnameT.setText("");
		dp.nameT.setText("");
		dp.initialsT.setText("");
		dp.idNumberT.setText("");
		dp.phoneOneT.setText("");
		dp.phoneTwoT.setText("");
		dp.emailT.setText("");
		dp.titleJCB.setSelectedItem("");
		dp.accessLevelJCB.setSelectedItem("");
		dp.jdc.setDate(null);
		dp.newPasswordB.setEnabled(false);
		ap1.streetT.setText("");
		ap1.suburbT.setText("");
		ap1.cityT.setText("");
		ap1.posCodeT.setText("");
		//searchT.setText("");
	}
	public static void main(String [] args)
	{
		new EmpTestFrame();
	}
}

class EmpTestFrame extends JFrame
{
	public EmpTestFrame()
	{	
		try
		{
			Connection conn;
			String userid, password;
			userid = "root";
			password = "";
			MySQL_Connection msc = new MySQL_Connection(
					"turtleData","localhost",userid, password);
			conn = msc.getMySQLConnection();
		
			EmpPanel cp = new EmpPanel(conn);
			this.setTitle("Employee frame");
			this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			this.getContentPane().add(cp, BorderLayout.CENTER);
			this.setSize(800,600);
			this.setLocationRelativeTo(null);
			this.setVisible(true);
		}
		catch(SQLException sqlx)
		{
			sqlx.printStackTrace();
		}
		catch(ClassNotFoundException cnfe)
		{
			cnfe.printStackTrace();
		}
	}
}